package com.corn.turorial.spring.controller;

import com.corn.turorial.spring.data.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2021/2/8 16:13
 */
@RestController
public class SqlTestController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") String id) {
        return jdbcTemplate.queryForObject("SELECT * FROM user WHERE id = " + id, new BeanPropertyRowMapper<User>(User.class));
    }

    @GetMapping("/findLike/{id}")
    public User findLike(@PathVariable("id") String id) {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT * FROM user WHERE id like CONCAT ('%',?,'%')", id);
        return jdbcTemplate.queryForObject("SELECT * FROM user WHERE id LIKE '%" + id + "%'", new BeanPropertyRowMapper<User>(User.class));
    }
}
